package cn.anecansaitin.whimsy.common.listener;

import cn.anecansaitin.whimsy.Whimsy;
import cn.anecansaitin.whimsy.common.capabilities.battle.IBattleMode;
import cn.anecansaitin.whimsy.common.capabilities.battle.IOWController;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.player.Player;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;

@Mod.EventBusSubscriber(modid = Whimsy.MODID)
public class PlayerHurt {
    @SubscribeEvent
    public static void hurt(LivingDamageEvent event) {
        if(!(event.getEntity() instanceof Player player)) {
            return;
        }

        //在玩家受伤前时先用拓展血量进行消耗
        IBattleMode battleMode = player.getCapability(IBattleMode.BATTLE_MODE);

        //处于战斗模式中才生效
        if (battleMode == null || !battleMode.isInMode()) {
            return;
        }

        if (!(battleMode.getController() instanceof IOWController controller)) {
            return;
        }

        float hurt = controller.hurt(player, event.getSource(), event.getAmount());
        player.sendSystemMessage(Component.literal("hp-e:" + controller.expandedHP().getTotalHP()));
        event.setAmount(hurt);
    }
}
